package com.dst.note;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.dst.note.dao.NoteDao;
import com.dst.note.model.Note;
import com.dst.note.R;
import com.dst.note.lisenter.ClockListener;

/**
 * 开始录音的 DialogFragment
 * <p>
 * Created by developerHaoz on 2017/8/12.
 */

public class ClockDialogFragment extends DialogFragment {

    private ClockListener mCancelListener;

    public void setCancelListener(ClockListener mCancelListener) {
        this.mCancelListener = mCancelListener;
    }

    public static ClockDialogFragment newInstance(String id) {
        ClockDialogFragment dialogFragment = new ClockDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putString("id", id);
        dialogFragment.setArguments(bundle);
        return dialogFragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCancelable(false);
        if (getDialog() != null) {
            getDialog().setCanceledOnTouchOutside(false);
        }
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_clock, null);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(false);
        initView(view);

        builder.setCancelable(false);
        builder.setView(view);
        return builder.create();
    }


    private void initView(View view) {
        TextView textView = view.findViewById(R.id.dai_ban_tv);
        Bundle bundle = getArguments();
        NoteDao noteDao = new NoteDao(getActivity());
        String id = bundle.getString("id");
        if (TextUtils.isEmpty(id)) {
            textView.setText("您有待办事项！");
        } else {
            Note note = noteDao.find(id);
            textView.setText(note.getTitle());
        }
        view.findViewById(R.id.confirm_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (mCancelListener != null) {
                    mCancelListener.confirm();
                }
            }
        });
        view.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (mCancelListener != null) {
                    mCancelListener.cancel();
                }
            }
        });
    }


}
